package com.atguigu.gulimall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.atguigu.gulimall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private   String app_id = "2021000118635767";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private  String merchant_private_key = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCRw2QmG5ZZRZ0DyM7TVQjEGRvZmiUScnriGCYhrTiWHmXvShePjC6wBdY2aABmGTHgKQVFjySQDnQwgHriGHRKY/VSa07f2numVcSURCIreOuE59KqAOqD1uqYwGW8pfjYSO5suOAh3oqIx3YdFzm/6X7c2VLHiTeN7oo5vw3MR1MWt7jiMPl4UCLITnhRQz14vM/8UiecDX6ZljOQ+Af06ipWY/KPMw1Yjo8UDRqWGHDLLeygBjD9gHsoqd0bO4wpRedaHxv6NJmGB45RsoR8JLpJ75e6nRD1mvdTKT8CP0GCLM/c1n3+Obue1V3XljbcEOqnGS4a8K3clTFCBtxLAgMBAAECggEAZOcEUdjxCBVHbxI4ElUf/1d+TvEEzrNO22pqIw/dRxX97Fq22pjozNMNiZYPCJ5SR2E+E/vWDJ7k0Zpu9kqFXeW6Mfa0oAOHawqeTSCpIrzPFK7BxwthTCifN2SgSKoADADYg4yrIucBsVOaMOMeB/qj5k/OnPAg8/B34SefbpO5kj+SPVboNhwxhaJf62ni1N59E+XP6sPVaM4lmPVI/DTNgKJROVqLym9nJLU8+a41Zs93vlP/2cu+2T0hDvgw8QVETJmeAtWUb3Px4uFBRwyAV8AWlVleW/Fm/baYB+ksyxEx96a+OL93rN/qLHhAJ1UZvD/ZPnz1mP2eLs8yyQKBgQDIXJmFmSmDnBrA0PaZwhA6uDAILXhEdmZEiNIMZB//j7vhCei5VyoLSxECozdwZopIItBTfZLqZ/K9HpiQemKZsPGEN7RFqqVwQiw7OqfhZAvnKv/ghOrj1ho2nbITUCZur/0z0f7X4bw4qMVudE3xo3150/Z4kP5+25DTw7AbLwKBgQC6PXjLOFHdOHNAz0YYy4LmriWPu48MBsKK/18r7+mwjMidYXOFlH0+oQWpoAMy6bTz/K6diBGKTL2KGMjH+adLyjCGZzLflcl6jObJ+ZXBZndkLMxpeJwBz/ILhXbG8fBwbj1TwJ+VAUbHbJZdaQkfiN3rqZEhsw8OxDyLbI5ZpQKBgQCQ7yMCmDOGh3J2zfHEcbPptfjQwYuBAqChrNM8sWTQKfn/JHrbLykzuB5a6RQeU55t5fkKnAidVYoqsaoswMr4YB2kxeK6jmFUHhn+0kREb2mI0/r6P0vmeabGoTYCcHUMlnOze5E8Ooc7+SGv76O+SsRmTyS4PDJMM6nE9YY7cwKBgHcoW3/SgfZDZuJBe7zjgNYs99VVkHmGXbRnHbJT5rXR7Y1cS3hdACExDxlvSnypsOAmfGZIJ8jPvrl7kAL6hYJlU9S6dH8/K9CgFA2IBbve3maWjxawV0iFjny2oL87DrYwokZSyQeh8HIp1K7hRDgL7skYDcZ89RZikcY254ENAoGBAKMN1DvFnlpFwDcr5waRSdbdvDaFEkE1JDEKkOgiu3DvPgm6GrAP3BOk6U0P6VN43J8TFpWpDRz95SkOpMe44iVPnxGczGyuXSN49bVpi3vdyxwAy4nIPeC3Az4v9YhSaA9u0YpqOBx5VGpceatJ25ONbmjrGQXlRJ0/a+7S24oR";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private  String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEApKbTxjx5B4Oj1jdSTFSaePr8wuSYAYjTveNMYAcbObjQgjrdlg28fpLvuYw5j0JGK4G33coP9YOk+iRVftg2UpwwLhRvBkjEPPIjHTtUz1lHtXeTxy4/EXqg4PQdKyrWGHkxOLP6q07yqWmJ8wXWh1pD1GBJxxT+R9Ov5nfJ+AcZca6R4C9hbZbK9Kk8KdBZl6e2gFE0jr+i015PF/MfpTAlsIarESnFoxEJ8JVWXoHF5mW5lPPcpBVydl7rMwJPaBRi9oWtWKKtSbyeFgqYwGYNfxZLdHynBWMxlV9VMKi9Vh8caGlkiUQAPLa0wUU0gY0+n5tiNVV490Y+4Gh/LwIDAQAB";
    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private  String notify_url;

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private  String return_url;

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    private String timeout = "1m";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"timeout_express\":\""+timeout+"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }
}
